package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUIDatagrid;
import com.ego.commons.pojo.EgoResult;
import com.ego.commons.utils.IDUtils;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.dubbo.service.TbItemParamItemDubboService;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemDesc;
import com.ego.pojo.TbItemParamItem;
import com.ego.rabbitmq.SendMessage;
import com.ego.service.TBItemService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Service
public class TBItemServiceImpl implements TBItemService {
    @DubboReference
    private TbItemDubboService tbItemDubboService;

    @Autowired
    private SendMessage sendMessage;

    @Value("${ego.operation.exchange}")
    private String exchange;

    @Value("${ego.operation.solrInsertRoutingKey}")
    private String solrInsertRoutingKey;

    @Value("${ego.operation.solrDeleteRoutingKey}")
    private String solrDeleteRoutingKey;
    @Override
    public EasyUIDatagrid showItem(int page, int size) {

        PageInfo<TbItem> tbItemPageInfo = tbItemDubboService.selectTbItemByPage(page, size);
        //获取结果集
        List<TbItem> items = tbItemPageInfo.getList();
        //获取总数
        long total = tbItemPageInfo.getTotal();

        EasyUIDatagrid easyUIDatagrid = new EasyUIDatagrid(total, items);

        return easyUIDatagrid;
    }

    @Override
    public EgoResult updateItemStatusByIds(long[] ids, int status) {
        try {
            tbItemDubboService.updateItemStatusByIds(ids, status);
            //上架
            if(status==1){
                //使用RabbitMQ发送消息，更新Solr索引引擎数据
                sendMessage.asyncSenderMessage(exchange,solrInsertRoutingKey,ids);
            }else if(status == 2 || status ==3){
                sendMessage.asyncSenderMessage(exchange,solrDeleteRoutingKey,ids);
            }

                return EgoResult.ok();
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
    }

    @Override
    public EgoResult insert(TbItem tbItem, String desc,String itemParams) {
        long itemId = IDUtils.genItemId();
        //设置商品id
        tbItem.setId(itemId);

        //创建商品描述对象
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        //创建商品规格参数对象
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);

        try {
            int row = tbItemDubboService.insert(tbItem, tbItemDesc,tbItemParamItem);
            if (row==1) {
                //使用RabbitMQ发送消息，更新Solr索引引擎数据
                sendMessage.asyncSenderMessage(exchange,solrInsertRoutingKey,itemId);
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("新增商品失败!");
    }

    @Override
    public EgoResult update(TbItem tbItem, String desc,String itemParams,long itemParamId) {
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        //创建商品规格参数对象
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setId(itemParamId);
        tbItemParamItem.setParamData(itemParams);
        try {
            int row = tbItemDubboService.update(tbItem, tbItemDesc,tbItemParamItem);
            if (row==1){
                //使用RabbitMQ发送消息，更新Solr索引引擎数据
                sendMessage.asyncSenderMessage(exchange,solrInsertRoutingKey,tbItem.getId());

                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
            return EgoResult.error(e.getMessage());
        }
        return EgoResult.error("修改商品失败!");
    }
}
